package com.gitee.funcy.multiple.datasource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.funcy.multiple.datasource.domain.User;
import com.gitee.funcy.multiple.datasource.domain.query.UserQuery;
import com.gitee.funcy.multiple.datasource.mapper.UserMapper;
import com.gitee.funcy.multiple.datasource.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ｛这里添加描述｝
 *
 * @author funcy
 * @date 2019-09-25 21:44
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 按query查询
     * @param query
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<User> selectByQuery(UserQuery query) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(null != query.getUid()) {
            queryWrapper.eq("uid", query.getUid());
        }
        if(StringUtils.isNotBlank(query.getNick())) {
            queryWrapper.eq("nick", query.getNick());
        }
        if(StringUtils.isNotBlank(query.getNickLike())) {
            queryWrapper.like("nick", query.getNickLike());
        }
        return userMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(String nick) {
        User user = new User();
        user.setNick(nick);
        int result = userMapper.insert(user);
        throw new RuntimeException("这里抛出一个异常");
    }

}
